﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;


namespace BenhVien
{
    public partial class fmDecentralization : Form
    {
        private Connection cons = new Connection();
        private DataTable dt = new DataTable();
        private DataTable dtShow = new DataTable();
        private SqlDataAdapter da = new SqlDataAdapter();

        public fmDecentralization()
        {
            InitializeComponent();
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            FormChinh _fmMain = new FormChinh();
            _fmMain.Show();
        }
        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = cons.con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select 
                                            CapQuyen.Vitri as N'Vitri',
                                            CapQuyen as N'CapQuyen',
                                            NhanSu as N'NhanSu',
                                            QuanLyNV as N'QuanLyNV',
                                            QuanLyTaiKhoan as N'QuanLyTaiKhoan',
                                            DanhSachNV as N'DanhSachNV',
                                            DanhSachTaiKhoan as N'DanhSachTaiKhoan',
                                            BenhNhan as N'BenhNhan',
                                            PhieuHen as N'PhieuHen',
                                            ThongtinBN as N'ThongtinBN',
                                            BenhNhanNgoaiTru as N'BenhNhanNgoaiTru',
                                            BenhNhanNoiTru as N'BenhNhanNoiTru',
                                            CapThuoc as N'CapThuoc',
                                            VatTu as N'VatTu',
                                            ThemVatTu as N'ThemVatTu',
                                            ChinhSuaVatTu as N'ChinhSuaVatTu',
                                            CapVatTu as N'CapVatTu',
                                            XemVatTuTrongKho as N'XemVatTuTrongKho',
                                            XemVatTuCapChoPhong as N'XemVatTuCapChoPhong', 
                                            BieuMauBaoCao as N'BieuMauBaoCao',
                                            LichLamViec as N'LichLamViec',
                                            TroGiup as N'TroGiup'                                           
                                            From    CapQuyen";
            da.SelectCommand = command;
            da.Fill(dt);
            
        }
        private void btUpdate_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("Vitri=\'"+cbChucVu.Text+"\'")[0];
            row.BeginEdit();
            if (ckCapQuyen.Checked) row["CapQuyen"] = "True"; else row["CapQuyen"] = "False";
            if (ckNhanSu.Checked) row["NhanSu"] = "True"; else row["NhanSu"] = "False";
            if (ckQuanLyNV.Checked) row["QuanLyNV"] = "True"; else row["QuanLyNV"] = "False";
            if (ckQuanLyTaiKhoan.Checked) row["QuanLyTaiKhoan"] = "True"; else row["QuanLyTaiKhoan"] = "False";
            if (ckDanhSachNV.Checked) row["DanhSachNV"] = "True"; else row["DanhSachNV"] = "False";
            if (ckDanhSachTaiKhoan.Checked) row["DanhSachTaiKhoan"] = "True"; else row["DanhSachTaiKhoan"] = "False";
            if (ckBenhNhan.Checked) row["BenhNhan"] = "True"; else row["BenhNhan"] = "False";
            if (ckPhieuHen.Checked) row["PhieuHen"] = "True"; else row["PhieuHen"] = "False";
            if (ckThongtinBN.Checked) row["ThongtinBN"] = "True"; else row["ThongtinBN"] = "False";
            if (ckBenhNhanNgoaiTru.Checked) row["BenhNhanNgoaiTru"] = "True"; else row["BenhNhanNgoaiTru"] = "False";
            if (ckBenhNhanNoiTru.Checked) row["BenhNhanNoiTru"] = "True"; else row["BenhNhanNoiTru"] = "False";
            if (ckCapThuoc.Checked) row["CapThuoc"] = "True"; else row["CapThuoc"] = "False";
            if (ckVatTu.Checked) row["VatTu"] = "True"; else row["VatTu"] = "False";
            if (ckThemVatTu.Checked) row["ThemVatTu"] = "True"; else row["ThemVatTu"] = "False";
            if (ckChinhSuaVatTu.Checked) row["ChinhSuaVatTu"] = "True"; else row["ChinhSuaVatTu"] = "False";
            if (ckCapVatTu.Checked) row["CapVatTu"] = "True"; else row["CapVatTu"] = "False";
            if (ckXemVatTuTrongKho.Checked) row["XemVatTuTrongKho"] = "True"; else row["XemVatTuTrongKho"] = "False";
            if (ckXemVatTuCapChoPhong.Checked) row["XemVatTuCapChoPhong"] = "True"; else row["XemVatTuCapChoPhong"] = "False";
            if (ckBieuMauBaoCao.Checked) row["BieuMauBaoCao"] = "True"; else row["BieuMauBaoCao"] = "False";
            if (ckLichLamViec.Checked) row["LichLamViec"] = "True"; else row["LichLamViec"] = "False";
            if (ckTroGiup.Checked) row["TroGiup"] = "True"; else row["TroGiup"] = "False";
            row.EndEdit();
            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = cons.con;
            commandUpdate.CommandType = CommandType.Text;
            commandUpdate.CommandText = @"Update CapQuyen Set CapQuyen=@CapQuyen,
                                                                NhanSu=@NhanSu,
                                                                QuanLyNV=@QuanLyNV,
                                                                QuanLyTaiKhoan=@QuanLyTaiKhoan,
                                                                DanhSachNV=@DanhSachNV,
                                                                DanhSachTaiKhoan=@DanhSachTaiKhoan,
                                                                BenhNhan=@BenhNhan,
                                                                PhieuHen=@PhieuHen,
                                                                ThongtinBN=@ThongtinBN,
                                                                BenhNhanNgoaiTru=@BenhNhanNgoaiTru,
                                                                BenhNhanNoiTru=@BenhNhanNoiTru,
                                                                CapThuoc=@CapThuoc,
                                                                VatTu=@VatTu,
                                                                ThemVatTu=@ThemVatTu,
                                                                ChinhSuaVatTu=@ChinhSuaVatTu,
                                                                CapVatTu=@CapVatTu,
                                                                XemVatTuTrongKho=@XemVatTuTrongKho,
                                                                XemVatTuCapChoPhong=@XemVatTuCapChoPhong,
                                                                BieuMauBaoCao=@BieuMauBaoCao,
                                                                LichLamViec=@LichLamViec,
                                                                TroGiup=@TroGiup
                                                                Where Vitri=@Vitri";
            commandUpdate.Parameters.Add("@Vitri",SqlDbType.NVarChar,50,"Vitri");
            commandUpdate.Parameters.Add("@CapQuyen", SqlDbType.Bit, 10, "CapQuyen");
            commandUpdate.Parameters.Add("@NhanSu", SqlDbType.Bit, 10, "NhanSu");
            commandUpdate.Parameters.Add("@QuanLyNV", SqlDbType.Bit, 10, "QuanLyNV");
            commandUpdate.Parameters.Add("@QuanLyTaiKhoan", SqlDbType.Bit, 10, "QuanLyTaiKhoan");
            commandUpdate.Parameters.Add("@DanhSachNV", SqlDbType.Bit, 10, "DanhSachNV");
            commandUpdate.Parameters.Add("@DanhSachTaiKhoan", SqlDbType.Bit, 10, "DanhSachTaiKhoan");
            commandUpdate.Parameters.Add("@BenhNhan", SqlDbType.Bit, 10, "BenhNhan");
            commandUpdate.Parameters.Add("@PhieuHen", SqlDbType.Bit, 10, "PhieuHen");
            commandUpdate.Parameters.Add("@ThongtinBN", SqlDbType.Bit, 10, "ThongtinBN");
            commandUpdate.Parameters.Add("@BenhNhanNgoaiTru", SqlDbType.Bit, 10, "BenhNhanNgoaiTru");
            commandUpdate.Parameters.Add("@BenhNhanNoiTru", SqlDbType.Bit, 10, "BenhNhanNoiTru");
            commandUpdate.Parameters.Add("@CapThuoc", SqlDbType.Bit, 10, "CapThuoc");
            commandUpdate.Parameters.Add("@VatTu", SqlDbType.Bit, 10, "VatTu");
            commandUpdate.Parameters.Add("@ThemVatTu", SqlDbType.Bit, 10, "ThemVatTu");
            commandUpdate.Parameters.Add("@ChinhSuaVatTu", SqlDbType.Bit, 10, "ChinhSuaVatTu");
            commandUpdate.Parameters.Add("@CapVatTu", SqlDbType.Bit, 10, "CapVatTu");
            commandUpdate.Parameters.Add("@XemVatTuTrongKho", SqlDbType.Bit, 10, "XemVatTuTrongKho");
            commandUpdate.Parameters.Add("@XemVatTuCapChoPhong", SqlDbType.Bit, 10, "XemVatTuCapChoPhong");
            commandUpdate.Parameters.Add("@BieuMauBaoCao", SqlDbType.Bit, 10, "BieuMauBaoCao");
            commandUpdate.Parameters.Add("@LichLamViec", SqlDbType.Bit, 10, "LichLamViec");
            commandUpdate.Parameters.Add("@TroGiup", SqlDbType.Bit, 10, "TroGiup");

            da.UpdateCommand = commandUpdate;
            da.Update(dt);
            MessageBox.Show("Edit success!", "MesageBox", MessageBoxButtons.OK);


        }

        private void fmDecentralization_Load(object sender, EventArgs e)
        {
            this.cbChucVu.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            cons.Connect();
            getdata();
            cbChucVu_SelectedIndexChanged(null, null);
            checkTick();
            
        }
        private void cbChucVu_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlCommand commandShow = new SqlCommand();
            commandShow.Connection = cons.con;
            commandShow.CommandType = CommandType.Text;
            commandShow.CommandText = "Select * from CapQuyen Where Vitri=\'" + cbChucVu.Text + "\'";
            da.SelectCommand = commandShow;
            dtShow.Clear();
            da.Fill(dtShow);
            ckCapQuyen.Checked = Boolean.Parse(dtShow.Rows[0][1].ToString());
            ckNhanSu.Checked = Boolean.Parse(dtShow.Rows[0][2].ToString());
            ckQuanLyNV.Checked = Boolean.Parse(dtShow.Rows[0][3].ToString());
            ckQuanLyTaiKhoan.Checked = Boolean.Parse(dtShow.Rows[0][4].ToString());
            ckDanhSachNV.Checked = Boolean.Parse(dtShow.Rows[0][5].ToString());
            ckDanhSachTaiKhoan.Checked = Boolean.Parse(dtShow.Rows[0][6].ToString());
            ckBenhNhan.Checked = Boolean.Parse(dtShow.Rows[0][7].ToString());
            ckPhieuHen.Checked = Boolean.Parse(dtShow.Rows[0][8].ToString());
            ckThongtinBN.Checked = Boolean.Parse(dtShow.Rows[0][9].ToString());
            ckBenhNhanNgoaiTru.Checked = Boolean.Parse(dtShow.Rows[0][10].ToString());
            ckBenhNhanNoiTru.Checked = Boolean.Parse(dtShow.Rows[0][11].ToString());
            ckCapThuoc.Checked = Boolean.Parse(dtShow.Rows[0][12].ToString());
            ckVatTu.Checked = Boolean.Parse(dtShow.Rows[0][13].ToString());
            ckThemVatTu.Checked = Boolean.Parse(dtShow.Rows[0][14].ToString());
            ckChinhSuaVatTu.Checked = Boolean.Parse(dtShow.Rows[0][15].ToString());
            ckCapVatTu.Checked = Boolean.Parse(dtShow.Rows[0][16].ToString());
            ckXemVatTuTrongKho.Checked = Boolean.Parse(dtShow.Rows[0][17].ToString());
            ckXemVatTuCapChoPhong.Checked = Boolean.Parse(dtShow.Rows[0][18].ToString());
            ckBieuMauBaoCao.Checked = Boolean.Parse(dtShow.Rows[0][19].ToString());
            ckLichLamViec.Checked = Boolean.Parse(dtShow.Rows[0][20].ToString());
            ckTroGiup.Checked = Boolean.Parse(dtShow.Rows[0][21].ToString());
        }

        private void btDefaut_Click(object sender, EventArgs e)
        {
            ckCapQuyen.Checked = false;
            ckNhanSu.Checked = true;
            ckQuanLyNV.Checked = false;
            ckQuanLyTaiKhoan.Checked = false;
            ckDanhSachNV.Checked = false;
            ckDanhSachTaiKhoan.Checked = false;
            ckBenhNhan.Checked = true;
            ckPhieuHen.Checked = false;
            ckThongtinBN.Checked = false;
            ckBenhNhanNgoaiTru.Checked = false;
            ckBenhNhanNoiTru.Checked = false;
            ckCapThuoc.Checked = false;
            ckVatTu.Checked = true;
            ckThemVatTu.Checked = false;
            ckChinhSuaVatTu.Checked = false;
            ckCapVatTu.Checked = false;
            ckXemVatTuTrongKho.Checked = false;
            ckXemVatTuCapChoPhong.Checked = false;
            ckBieuMauBaoCao.Checked = false;
            ckLichLamViec.Checked = true;
            ckTroGiup.Checked = true;
        }

        private void ckQuanLyNV_CheckedChanged(object sender, EventArgs e)
        {
            if (ckQuanLyNV.Checked) ckNhanSu.Checked = true; 
        }

        private void ckQuanLyTaiKhoan_CheckedChanged(object sender, EventArgs e)
        {
            if (ckQuanLyTaiKhoan.Checked) ckNhanSu.Checked = true; 
        }

        private void ckDanhSachNV_CheckedChanged(object sender, EventArgs e)
        {
            if (ckDanhSachNV.Checked) ckNhanSu.Checked = true; 
        }

        private void ckDanhSachTaiKhoan_CheckedChanged(object sender, EventArgs e)
        {
            if (ckDanhSachTaiKhoan.Checked) ckNhanSu.Checked = true; 
        }

        private void ckPhieuHen_CheckedChanged(object sender, EventArgs e)
        {
            if (ckPhieuHen.Checked) ckBenhNhan.Checked = true; 
        }

        private void ckThongtinBN_CheckedChanged(object sender, EventArgs e)
        {
            if (ckThongtinBN.Checked) ckBenhNhan.Checked = true; 
        }

        private void ckBenhNhanNgoaiTru_CheckedChanged(object sender, EventArgs e)
        {
            if (ckBenhNhanNgoaiTru.Checked) ckBenhNhan.Checked = true; 
        }

        private void ckBenhNhanNoiTru_CheckedChanged(object sender, EventArgs e)
        {
            if (ckBenhNhanNoiTru.Checked) ckBenhNhan.Checked = true; 
        }

        private void ckCapThuoc_CheckedChanged(object sender, EventArgs e)
        {
            if (ckCapThuoc.Checked) ckBenhNhan.Checked = true; 
        }

        private void ckThemVatTu_CheckedChanged(object sender, EventArgs e)
        {
            if (ckThemVatTu.Checked) ckVatTu.Checked = true; 
        }

        private void ckChinhSuaVatTu_CheckedChanged(object sender, EventArgs e)
        {
            if (ckChinhSuaVatTu.Checked) ckVatTu.Checked = true; 
        }

        private void ckCapVatTu_CheckedChanged(object sender, EventArgs e)
        {
            if (ckCapVatTu.Checked) ckVatTu.Checked = true; 
        }

        private void ckXemVatTuTrongKho_CheckedChanged(object sender, EventArgs e)
        {
            if (ckXemVatTuTrongKho.Checked) ckVatTu.Checked = true; 
        }

        private void ckXemVatTuCapChoPhong_CheckedChanged(object sender, EventArgs e)
        {
            if (ckXemVatTuCapChoPhong.Checked) ckVatTu.Checked = true; 
        }
        private void checkTick()
        {
            ckQuanLyNV_CheckedChanged(null, null);
            ckQuanLyTaiKhoan_CheckedChanged(null, null);
            ckDanhSachNV_CheckedChanged(null, null);
            ckDanhSachTaiKhoan_CheckedChanged(null, null);
            ckPhieuHen_CheckedChanged(null, null);
            ckThongtinBN_CheckedChanged(null, null);
            ckBenhNhanNgoaiTru_CheckedChanged(null, null);

            ckBenhNhanNoiTru_CheckedChanged(null, null);


            ckCapThuoc_CheckedChanged(null, null);

            ckThemVatTu_CheckedChanged(null, null);


            ckChinhSuaVatTu_CheckedChanged(null, null);

            ckCapVatTu_CheckedChanged(null, null);


            ckXemVatTuTrongKho_CheckedChanged(null, null);


            ckXemVatTuCapChoPhong_CheckedChanged(null, null);
        }
    }
}
